/* Siberian Olympiad. 2 stage.
  Problemset: http://agul-problems1.googlecode.com/svn/trunk/2010-2011/wso/stage2/wso_2stage.pdf
  Problem 9.
  Points: 22
*/
#include <set>
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;
int n,span_r[5555],span_k[5555],sp;
multiset <int> mas[5555],kletka[11111];
void go_back_yeah (int tek_size, multiset<int>::iterator tek) {
	multiset<int>::iterator f=kletka[tek_size].find(*tek);
	if (f!=kletka[tek_size].end()) {
		kletka[tek_size].erase(f);
		mas[tek_size].erase(tek);
		return;
	}
	for (multiset<int>::iterator i=mas[tek_size-1].begin();i!=mas[tek_size-1].end() &&
	     i!=++mas[tek_size-1].end();i++)
		if (*tek == *i + *(++i)) {
			mas[tek_size].erase(tek);
			go_back_yeah (tek_size-1,i);
			go_back_yeah (tek_size-1,--i);
			return;
		}
}


int main () {
	freopen("input.txt","r",stdin);
	freopen("output.txt","w",stdout);
	scanf("%d",&n);
	for (int i=0;i<n;i++) {
		int a,b;
		scanf("%d%d",&a,&b);
		kletka[a].insert(b);
	}
	scanf("%d",&sp);
	int maximum=0;
	for (int i=0;i<sp;i++) {
		scanf("%d%d",&span_r[i],&span_k[i]);	
		maximum=max(maximum,span_r[i]);
	}
	for (int tek=1;tek <= maximum;tek++) {
		for (multiset<int>::iterator i=kletka[tek].begin();i!=kletka[tek].end();i++) 
			mas[tek].insert(*i);
		for (multiset<int>::iterator i=mas[tek-1].begin();i!=mas[tek-1].end() && i!=++mas[tek-1].end();i++) 
			mas[tek].insert(*i + *(++i));
	}
	int itog=0;
	for (int cont=0;cont<sp;cont++) {
		for (int i=0;i<span_k[cont];i++) {
			if (mas[span_r[cont]].empty()) {
				printf("NO");
				return 0;
			}
			itog+=*mas[span_r[cont]].begin();
			go_back_yeah (span_r[cont],mas[span_r[cont]].begin());
		}
	}
	printf("%d",itog);
	return 0;
}
